package drds.global_transaction.transaction_coordinator.transaction_session;

import drds.common.ShouldNeverHappenException;
import drds.global_transaction.api.exception.GlobalTransactionException;

import java.io.IOException;

public class TransactionSessionManagerFactory
{

	private static final String root_transaction_session_manager_name = "root.transaction.transaction_session.manager" + ".name";
	private static final String async_transaction_session_manager_name = "async.transaction.transaction_session" + ".manager.name";
	private static final String retry_committing_transaction_session_manager_name = "retry.committing.transaction" + ".transaction_session.manager.name";
	private static final String retry_rollbacking_transaction_session_manager_name = "retry.rollbacking.transaction" + ".transaction_session.manager.name";
	//
	private static TransactionSessionManager rootTransactionSessionManager;//
	private static TransactionSessionManager asynchronousTransactionSessionManager;
	private static TransactionSessionManager retryCommitTransactionSessionManager;
	private static TransactionSessionManager retryRollbackTransactionSessionManager;

	//
	public static GlobalTransactionSession findGlobalTransactionSession(Long globalTransactionId) throws GlobalTransactionException
	{
		return getRootTransactionSessionManager().getGlobalTransactionSession(globalTransactionId);
	}

	public static final TransactionSessionManager getAsynchronousTransactionSessionManager()
	{
		if (asynchronousTransactionSessionManager == null)
		{
			throw new ShouldNeverHappenException("TransactionSessionManager is NOT init!");
		}
		return asynchronousTransactionSessionManager;
	}

	public static final TransactionSessionManager getRetryCommitTransactionSessionManager()
	{
		if (retryCommitTransactionSessionManager == null)
		{
			throw new ShouldNeverHappenException("TransactionSessionManager is NOT init!");
		}
		return retryCommitTransactionSessionManager;
	}

	public static final TransactionSessionManager getRetryRollbackTransactionSessionManager()
	{
		if (retryRollbackTransactionSessionManager == null)
		{
			throw new ShouldNeverHappenException("TransactionSessionManager is NOT init!");
		}
		return retryRollbackTransactionSessionManager;
	}

	public static final TransactionSessionManager getRootTransactionSessionManager()
	{
		if (rootTransactionSessionManager == null)
		{
			throw new ShouldNeverHappenException("TransactionSessionManager is NOT init!");
		}
		return rootTransactionSessionManager;
	}

	//
	public static void init(String transactionSessionManagerStorePath) throws IOException
	{
		if (transactionSessionManagerStorePath == null)
		{
			rootTransactionSessionManager = new DefaultTransactionSessionManager(root_transaction_session_manager_name);
			asynchronousTransactionSessionManager = new DefaultTransactionSessionManager(async_transaction_session_manager_name);
			retryCommitTransactionSessionManager = new DefaultTransactionSessionManager(retry_committing_transaction_session_manager_name);
			retryRollbackTransactionSessionManager = new DefaultTransactionSessionManager(retry_rollbacking_transaction_session_manager_name);
		} else
		{
			if (!transactionSessionManagerStorePath.endsWith("/"))
			{
				transactionSessionManagerStorePath = transactionSessionManagerStorePath + "/";
			}
			rootTransactionSessionManager = new FileBasedTransactionSessionManager(root_transaction_session_manager_name, transactionSessionManagerStorePath);
			asynchronousTransactionSessionManager = new DefaultTransactionSessionManager(async_transaction_session_manager_name);
			retryCommitTransactionSessionManager = new DefaultTransactionSessionManager(retry_committing_transaction_session_manager_name);
			retryRollbackTransactionSessionManager = new DefaultTransactionSessionManager(retry_rollbacking_transaction_session_manager_name);

		}

	}
}
